/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uy.edu.ort.ws.adminFuente;

import java.net.URL;
import java.util.Properties;
import uy.edu.ort.dominio.Fuente;
import uy.edu.ort.recepcionar.administracion.RecepcionarDatosAdminWS;
import uy.edu.ort.recepcionar.administracion.RecepcionarDatosAdminWS_Service;

/**
 *
 * @author Gonza
 */
public class VentanaAdminFuentes extends javax.swing.JFrame {

    /**
     * Creates new form VentanaAdminFuentes
     */
    private static URL url = null;

    public VentanaAdminFuentes() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelCrearFuente = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNombreFuente = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtNombrePersona = new javax.swing.JTextField();
        txtUsuario = new javax.swing.JTextField();
        txtContrasena = new javax.swing.JTextField();
        jButtonCrear = new javax.swing.JToggleButton();
        jToggleButton2 = new javax.swing.JToggleButton();
        jLabelMensajesCrear = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtIdFuenteEliminar = new javax.swing.JTextField();
        jLabelMensajeEliminar = new javax.swing.JLabel();
        jButtonEliminar = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtUsuarioEliminar = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtContrasenaEliminar = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        txtIdFuenteAutorizar = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        txtCodigoAutorizacionNuevo = new javax.swing.JTextField();
        jButtonAutorizar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabelMensajeAutorizar = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtUsuarioAutorizar = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        txtContrasenaAutorizar = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Mantenimiento de Fuentes");

        jPanelCrearFuente.setName("Crear Fuente"); // NOI18N

        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("Nombre Fuente");
        jPanel1.add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));
        jPanel1.add(txtNombreFuente, new org.netbeans.lib.awtextra.AbsoluteConstraints(137, 11, 277, -1));

        jLabel2.setText("Nombre Persona");
        jPanel1.add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 52, -1, -1));

        jLabel3.setText("Usuario");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 90, -1, -1));

        jLabel4.setText("Contraseña");
        jPanel1.add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 128, -1, -1));
        jPanel1.add(txtNombrePersona, new org.netbeans.lib.awtextra.AbsoluteConstraints(137, 49, 277, -1));
        jPanel1.add(txtUsuario, new org.netbeans.lib.awtextra.AbsoluteConstraints(137, 87, 277, -1));
        jPanel1.add(txtContrasena, new org.netbeans.lib.awtextra.AbsoluteConstraints(137, 125, 277, -1));

        jButtonCrear.setText("Crear");
        jButtonCrear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCrearActionPerformed(evt);
            }
        });
        jPanel1.add(jButtonCrear, new org.netbeans.lib.awtextra.AbsoluteConstraints(137, 173, -1, -1));

        jToggleButton2.setText("Cancelar");
        jPanel1.add(jToggleButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(339, 173, -1, -1));
        jPanel1.add(jLabelMensajesCrear, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 236, 304, 24));

        jPanelCrearFuente.addTab("Crear Fuente", jPanel1);

        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel5.setText("Identificador Fuente");
        jPanel2.add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 14, -1, -1));
        jPanel2.add(txtIdFuenteEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(126, 11, 224, -1));
        jPanel2.add(jLabelMensajeEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 169, 213, 29));

        jButtonEliminar.setText("Eliminar");
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });
        jPanel2.add(jButtonEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(126, 140, -1, -1));

        jButton2.setText("Cancelar");
        jPanel2.add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(275, 140, -1, -1));

        jLabel6.setText("Usuario");
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 45, -1, -1));
        jPanel2.add(txtUsuarioEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(126, 42, 224, -1));

        jLabel7.setText("Contraseña");
        jPanel2.add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 76, -1, -1));
        jPanel2.add(txtContrasenaEliminar, new org.netbeans.lib.awtextra.AbsoluteConstraints(126, 76, 224, -1));

        jPanelCrearFuente.addTab("Eliminar Fuente", jPanel2);

        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel8.setText("Identificador Fuente");
        jPanel3.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 14, -1, -1));
        jPanel3.add(txtIdFuenteAutorizar, new org.netbeans.lib.awtextra.AbsoluteConstraints(155, 11, 172, -1));

        jLabel9.setText("Código de autorización");
        jPanel3.add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 52, -1, -1));
        jPanel3.add(txtCodigoAutorizacionNuevo, new org.netbeans.lib.awtextra.AbsoluteConstraints(155, 49, 172, -1));

        jButtonAutorizar.setText("Autorizar");
        jButtonAutorizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAutorizarActionPerformed(evt);
            }
        });
        jPanel3.add(jButtonAutorizar, new org.netbeans.lib.awtextra.AbsoluteConstraints(155, 158, -1, -1));

        jButton3.setText("Cancelar");
        jPanel3.add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(252, 158, -1, -1));
        jPanel3.add(jLabelMensajeAutorizar, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 187, 355, 24));

        jLabel10.setText("Usuario");
        jPanel3.add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 88, -1, -1));
        jPanel3.add(txtUsuarioAutorizar, new org.netbeans.lib.awtextra.AbsoluteConstraints(155, 85, 172, -1));

        jLabel11.setText("Contraseña");
        jPanel3.add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 122, -1, -1));
        jPanel3.add(txtContrasenaAutorizar, new org.netbeans.lib.awtextra.AbsoluteConstraints(155, 119, 172, -1));

        jPanelCrearFuente.addTab("Autorizar Fuente", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelCrearFuente, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelCrearFuente, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanelCrearFuente.getAccessibleContext().setAccessibleName("Crear Fuente");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonCrearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCrearActionPerformed
        // TODO add your handling code here:
        if (url != null) {
            RecepcionarDatosAdminWS service = (RecepcionarDatosAdminWS) new RecepcionarDatosAdminWS_Service(url).getRecepcionarDatosAdminWSPort();

            String idAsignado = service.crearFuente(txtNombreFuente.getText(), txtNombrePersona.getText(), txtUsuario.getText(), txtContrasena.getText());
            jLabelMensajesCrear.setText("Número de identificador asignado es: " + idAsignado);
            //Limpio los campos luego de la creación
            txtNombreFuente.setText("");
            txtNombrePersona.setText("");
            txtUsuario.setText("");
            txtContrasena.setText("");
    }//GEN-LAST:event_jButtonCrearActionPerformed

        else {
            jLabelMensajesCrear.setText("No se encuentra la URL");
        }
        System.out.println("imprimo antes de mandar " + txtNombreFuente.getText() + txtNombrePersona.getText() + txtUsuario.getText() + txtContrasena.getText());
    }

    private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed
        if (url != null) {
            RecepcionarDatosAdminWS service = (RecepcionarDatosAdminWS) new RecepcionarDatosAdminWS_Service(url).getRecepcionarDatosAdminWSPort();

            String retorno = service.eliminarFuente(Long.valueOf(txtIdFuenteEliminar.getText()), txtUsuarioEliminar.getText(), txtContrasenaEliminar.getText());
            jLabelMensajeEliminar.setText(retorno);
            //Limpio los campos luego de la creacion
            txtIdFuenteEliminar.setText("");
            txtUsuarioEliminar.setText("");
            txtContrasenaEliminar.setText("");

        } else {
            jLabelMensajesCrear.setText("No se encuentra la URL");
        }
    }//GEN-LAST:event_jButtonEliminarActionPerformed

    private void jButtonAutorizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAutorizarActionPerformed
        if (url != null) {
            RecepcionarDatosAdminWS service = (RecepcionarDatosAdminWS) new RecepcionarDatosAdminWS_Service(url).getRecepcionarDatosAdminWSPort();
            String retorno = service.autorizarFuente(Long.valueOf(txtIdFuenteAutorizar.getText()), txtCodigoAutorizacionNuevo.getText(), txtUsuarioAutorizar.getText(), txtContrasenaAutorizar.getText());

            jLabelMensajeAutorizar.setText(retorno);

            //Limpio los campos luego de autorizar.
            txtIdFuenteAutorizar.setText("");
            txtCodigoAutorizacionNuevo.setText("");
            txtUsuarioAutorizar.setText("");
            txtContrasenaAutorizar.setText("");
        } else {
            jLabelMensajeAutorizar.setText("No se encuentra la URL");
        }
    }//GEN-LAST:event_jButtonAutorizarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VentanaAdminFuentes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VentanaAdminFuentes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VentanaAdminFuentes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VentanaAdminFuentes.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaAdminFuentes().setVisible(true);
            }
        });

        //url = new URL("http://localhost:8080/ServiciosAgenciaWS/ServiciosAgenciaWS?wsdl");
        //Leo la URL desde el archivo de configuración
        try {
            LeerArchivo archivoPropiedades = new LeerArchivo();
            Properties prop = archivoPropiedades.getArchivoProperties();
            try {
                url = new URL(archivoPropiedades.getProperty(prop, "URL"));
            } catch (Exception e) {
                System.out.println("No se encuentra la clave URL en el archivo de propiedades");

            }
        } catch (Exception ee) {
            System.out.println("No se puede abrir el archivo properties");
        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButtonAutorizar;
    private javax.swing.JToggleButton jButtonCrear;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelMensajeAutorizar;
    private javax.swing.JLabel jLabelMensajeEliminar;
    private javax.swing.JLabel jLabelMensajesCrear;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTabbedPane jPanelCrearFuente;
    private javax.swing.JToggleButton jToggleButton2;
    private javax.swing.JTextField txtCodigoAutorizacionNuevo;
    private javax.swing.JTextField txtContrasena;
    private javax.swing.JTextField txtContrasenaAutorizar;
    private javax.swing.JTextField txtContrasenaEliminar;
    private javax.swing.JTextField txtIdFuenteAutorizar;
    private javax.swing.JTextField txtIdFuenteEliminar;
    private javax.swing.JTextField txtNombreFuente;
    private javax.swing.JTextField txtNombrePersona;
    private javax.swing.JTextField txtUsuario;
    private javax.swing.JTextField txtUsuarioAutorizar;
    private javax.swing.JTextField txtUsuarioEliminar;
    // End of variables declaration//GEN-END:variables
}
